<?php
class badge extends CI_Controller {
	function index() {
		$this -> pub -> logp('浏览徽章', '1-2');
		$da = $this -> pub -> get_setting('徽章', 'Badge');
		$this -> load -> view('adminx/badge', $da);
	}

	function get() {
		$this -> pub -> logp('获取徽章数据', '1-2');
		if ($this -> input -> post('key')) {
			$this -> db -> select('count(*)');
			$this -> db -> like('name', $this -> input -> post('key'));
			$q = $this -> db -> get_where('co_badge');
			$ls = $q -> row_array(0);
			$num = $ls['count(*)'];
			$this -> db -> like('name', $this -> input -> post('key'));
		} else {
			$this -> db -> select('count(*)');
			$q = $this -> db -> get_where('co_badge');
			$ls = $q -> row_array(0);
			$num = $ls['count(*)'];
		}
		//一共的页数
		$da['num'] = ceil($num / 30);

		$page = is_numeric($this -> input -> post('page')) && $this -> input -> post('page') > 0 ? $this -> input -> post('page') : 1;
		$page = $page > $da['num'] ? $da['num'] : $page;
		$da['page'] = $page;

		$page = ($page - 1) == -1 ? 0 : ($page - 1);

		$this -> db -> limit(30, $page * 30);

		$this -> db -> order_by('id', 'desc');
		$q = $this -> db -> get_where('co_badge');
		$da['content'] = '';
		foreach ($q->result_array() as $v) {
			$da['content'] .= '<tr>';
			$da['content'] .= '<td>' . $v['id'] . '</td>';
			$da['content'] .= '<td><img width="48" src="' . $this -> pub -> oss_dz($v['pic']) . '" /></td>';
			$da['content'] .= '<td>' . $v['name'] . '</td>';

			$this -> db -> select('count(*)');
			$qq = $this -> db -> get_where('co_badges', array('hid' => $v['id']));
			$ls = $qq -> row_array(0);

			$da['content'] .= '<td>' . $ls['count(*)'] . '</td>';

			$da['content'] .= '<td>' . ($v['mktime'] ? date('Y-m-d', $v['mktime']) : '') . '</td>';
			$da['content'] .= '<td><button onclick="show_up(\'' . $v['id'] . '\')" type="button" class="btn default"><i class="fa fa-edit"></i>修改</button>';

			$da['content'] .= '<a href="/iadmin/send_badge/dox/' . $v['id'] . '" class="btn default blue"><i class="fa fa-send"></i>徽章发放</a>';

			$da['content'] .= '</td></tr>';
		}
		$da['type'] = 1;
		echo json_encode($da);
	}

	function add() {
		$this -> pub -> logp('添加徽章数据', '1-2');
		$da['name'] = $this -> input -> post('name');
		$da['jj'] = $this -> input -> post('jj');
		$da['je'] = $this -> input -> post('je');
		$da['numb'] = $this -> input -> post('numb');
		$da['pic'] = $this -> input -> post('pic');
		$da['mktime'] = mktime();

		$da['yq_sp'] = $this -> input -> post('yq_sp');
		$da['is_send'] = $this -> input -> post('is_send');
		$da['je'] = $this -> input -> post('je');
		$da['bfb'] = $this -> input -> post('bfb');
		$da['yq_numb'] = $this -> input -> post('yq_numb');
		$da['yq_je'] = $this -> input -> post('yq_je');

		$this -> db -> insert('co_badge', $da);
		echo json_encode(array('type' => 1));
	}

	function up() {
		$this -> pub -> logp('修改徽章数据 - ' . $this -> input -> post('idx'), '1-2');
		$da['name'] = $this -> input -> post('name');
		$da['jj'] = $this -> input -> post('jj');
		$da['je'] = $this -> input -> post('je');
		$da['numb'] = $this -> input -> post('numb');
		if ($this -> input -> post('pic')) {
			$da['pic'] = $this -> input -> post('pic');
		}
		$da['yq_sp'] = $this -> input -> post('yq_sp');
		$da['is_send'] = $this -> input -> post('is_send');
		$da['je'] = $this -> input -> post('je');
		$da['bfb'] = $this -> input -> post('bfb');
		$da['yq_numb'] = $this -> input -> post('yq_numb');
		$da['yq_je'] = $this -> input -> post('yq_je');
		$this -> db -> update('co_badge', $da, array('id' => $this -> input -> post('idx')));
		echo json_encode(array('type' => 1));
	}

	function get_one() {
		$this -> pub -> logp('读取徽章数据 - ' . $this -> input -> post('idx'), '1-2');
		$q = $this -> db -> get_where('co_badge', array('id' => $this -> input -> post('idx')));
		if ($q -> num_rows() > 0) {
			$da = $q -> row_array(0);
			$da['typex'] = 1;
			echo json_encode($da);
		} else {
			echo json_encode(array('typex' => 2));
		}
	}

}
?>